<?php

/**
 * Description of g
 * 
 * @author 叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */

namespace c\area;

class g extends \control
{
    var $table = 'area';
    
    /**
	 * 自动填充
	 * 
	 */
    function index_searchWord()
    {
    	$pName = '';
    	$pID = 0;
    	$keyA = explode(' ', $this->db->escape(ltrim($_GET['w'])) );
    	foreach ($keyA as $w)
    	 {  	 	
    	 	$res = $this->db->get($this->table, "name,id,py,  '$pName' as pName", "parent_id=$pID AND (name like '$w%' or py like '$w%')");
			if ($res && $w)
			 {
				$pID = $res[0]['id'];
				$pName .= $this->db->escape($res[0]['name']) .' ';
			 } 	 	
    	 }

    	ajax_json(array('status'=>TRUE, 'res'=>$res));
    }

	// 转换
	function toName($id)
	{
		if(!$this->_R)
			$this->_R = \YF_f::load_cache('user\domain\g');
		
		$id = intval($id);
		$n = '';
		while ($id){
			$r = $this->_R[$id];
			$n = $r['name'].' '. $n;
			$id = $r['parent_id'];
		 }
		return trim($n);
	}
	function toID($n)
	{
		$na = explode(' ', $this->db->escape($n) );
		$id = 0;
		foreach($na as $v)
		 	$id = $this->db->getOneField($this->table, 'id', "name='$v' AND parent_id=$id");
		return $id;
	}
	// cache
	function c_caching()
	{
		$r = $this->db->get($this->table, 'id,name,parent_id');
		$a = array_column($r, null, 'id');
		\YF_f::cache_file('user/domain/g.php', $a);
	}
}
